﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ANTProductAssemble_ManageAssemble_AssembleDetailAdd : System.Web.UI.Page
{
    private TAssembleDetail amd;
    private DataRow dr;
    private DataTable amddt
    {
        get
        {
            return (DataTable)ViewState["amddt"];
        }
        set
        {
            ViewState["amddt"] = value;
        }
    }

    public ANTProductAssemble_ManageAssemble_AssembleDetailAdd()
    {
        amd = new TAssembleDetail();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            amddt = new DataTable();
            amddt.Columns.Add(new DataColumn("BatchID", Type.GetType("System.String")));
            amddt.Columns.Add(new DataColumn("StuffTypeID", Type.GetType("System.String")));
            amddt.Columns.Add(new DataColumn("Remark", Type.GetType("System.String")));
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        dr = amddt.NewRow();
        dr["BatchID"] = this.TextBox2.Text.Trim();
        dr["StuffTypeID"] = this.TextBox3.Text.Trim();
        dr["Remark"] = this.TextBox6.Text.Trim();
        if (amddt.Select("BatchID = '" + this.TextBox2.Text.Trim() + "'").Length == 0)
        {
            //替换同一StuffTypeID的物料信息
            if (amddt.Select("StuffTypeID = '" + this.TextBox3.Text.Trim() + "'").Length > 0)
                amddt.Select("StuffTypeID = '" + this.TextBox3.Text.Trim() + "'")[0].Delete();
            amddt.Rows.InsertAt(dr, 0);
            Bindamd();
        }
        else
        {
            string str = "已经在列表中";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
    }

    protected void Bindamd()
    {
        this.GridView1.DataSource = amddt;
        this.GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[3].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除!')");
            }
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        amddt.Select("BatchID = '" + GridView1.Rows[e.RowIndex].Cells[0].Text + "'")[0].Delete();
        Bindamd();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (amddt.Rows.Count < 1)
        {
            string str = "请先添加关键物料信息!";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
        }
        else
        {
            //此处修改，MO与productTypeID不再在此录入
            string productTypeID = this.TextBox4.Text.Trim();
            string mOID = this.TextBox5.Text.Trim();
            //MOBuildandAdd(productTypeID, mOID);

            string productSN = this.TextBox1.Text.Trim();
            //HouseBuildandAdd(mOID, productSN);

            DateTime dt = DateTime.Now;
            TAssembleDetailInfo adi = new TAssembleDetailInfo(productSN, dt, User.Identity.Name);
            if (amd.TAssembleDetailItems_Insert(adi, amddt))
            {
                this.TextBox1.Text = "";
                this.TextBox1.Focus();
                string str = productSN + " 提交成功!";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
            else
            {
                string str = "提交失败，请联系相关人员!";
                ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str + "');</script>", false);
            }
        }        
    }

    //protected void MOBuildandAdd(string productTypeID, string mOID)
    //{
    //    TMOInfo moi = new TMOInfo(mOID, productTypeID);
    //    TMO m = new TMO();
    //    m.Insert(moi);
    //}

    //protected void HouseBuildandAdd(string mOID, string productSN)
    //{
    //    THouseInfo hi = new THouseInfo(productSN, mOID);
    //    THouse h = new THouse();
    //    h.Insert(hi);
    //}
}
